###################################
# --------- Setting up Datasets ----------####
###################################

aic_clean <- aic_df %>%
            filter(!(outcome==2.5|outcome==3.5|outcome==4.5|outcome==99)) %>%
            filter(issue <=99) %>%
            filter(ideological_code <= 99)

aic_clean$issue[aic_clean$issue > 31] <- NA
aic_clean$ideological_code[aic_clean$ideological_code > 7] <- NA

aic_clean$ideological_code <- ifelse(aic_clean$ideo_2 == 5 & aic_clean$norm_reverse2 == 1 , 3,
                              ifelse(aic_clean$ideo_2 == 6 & aic_clean$norm_reverse2 == 1 , 2 ,
                              ifelse(aic_clean$ideo_2 == 7 & aic_clean$norm_reverse2 == 1 , 1 ,
                              ifelse(aic_clean$ideo_2 == 1 & aic_clean$norm_reverse2 == 1 , 7 ,
                              ifelse(aic_clean$ideo_2 == 2 & aic_clean$norm_reverse2 == 1 , 6 ,
                              ifelse(aic_clean$ideo_2 == 3 & aic_clean$norm_reverse2 == 1 , 5 ,
                              ifelse(aic_clean$ideo_2 == 5 & aic_clean$norm_reverse2 == 0 , 5,
                              ifelse(aic_clean$ideo_2 == 6 & aic_clean$norm_reverse2 == 0 , 6 ,
                              ifelse(aic_clean$ideo_2 == 7 & aic_clean$norm_reverse2 == 0 , 7 ,
                              ifelse(aic_clean$ideo_2 == 1 & aic_clean$norm_reverse2 == 0 , 1 ,
                              ifelse(aic_clean$ideo_2 == 2 & aic_clean$norm_reverse2 == 0 , 2 ,
                              ifelse(aic_clean$ideo_2 == 3 & aic_clean$norm_reverse2 == 0 , 3 , 4))))))))))))

aic_clean$rescaled_ideol_code<-(aic_clean$ideological_code-1)/6
aic_clean$ne.policies<-0
aic_clean$ne.policies[aic_clean$fp.policies==0 & aic_clean$econ.policies==0]<-1
stopifnot(all((aic_clean$fp.policies+aic_clean$econ.policies+aic_clean$ne.policies)==1))


####################
#Time periods
####################

#Generate Samples for Divided vs. Unified Government
dem_control_years <- c(1987,1988,1989,1990,1991,1992,1993,1994)
rep_control_years <- c(1995,1996,1997,1998,1999,2000)

aic_clean$dem_control <- ifelse(aic_clean$year %in% dem_control_years, 1, 0)
aic_clean$rep_control <- ifelse(aic_clean$year %in% rep_control_years, 1, 0)

divided.congress <- filter(aic_clean, dem_control == 0 & rep_control == 0)
dem.congress <- filter(aic_clean, dem_control == 1 & rep_control == 0)
rep.congress <- filter(aic_clean, dem_control == 0 & rep_control == 1)

#String variable for congressional control
aic_clean$GovtControl<-" "
aic_clean$GovtControl[aic_clean$dem_control==1 & aic_clean$rep_control==0]<-"Dem"
aic_clean$GovtControl[aic_clean$rep_control==1 & aic_clean$dem_control==0]<-"Rep"
aic_clean$GovtControl[aic_clean$dem_control==0 & aic_clean$rep_control==0]<-"Divided"
stopifnot(!any(aic_clean$GovtControl==" "))

#Generate Samples for Democratic or Republican president
rep_pres_years <- c(1981:1992,2001:2002)
dem_pres_years <- c(1993:2000)

aic_clean$dem_pres <- ifelse(aic_clean$year %in% dem_pres_years, 1, 0)
aic_clean$rep_pres <- ifelse(aic_clean$year %in% rep_pres_years, 1, 0)

dem.pres <- filter(aic_clean, dem_pres == 1 & rep_pres == 0)
rep.pres <- filter(aic_clean, dem_pres == 0 & rep_pres == 1)

#String variable for presidential party
aic_clean$PresControl<-" "
aic_clean$PresControl[aic_clean$dem_pres==1 & aic_clean$rep_pres==0]<-"Dem"
aic_clean$PresControl[aic_clean$rep_pres==1 & aic_clean$dem_pres==0]<-"Rep"
stopifnot(!any(aic_clean$PresControl==" "))

#Generate Samples for each Issue Area
econ.codes <- c(1,3,4,5,8,10,13,14,15,17,21)
fp.codes <- c(9,16,18,19)
econ.policies <- filter(aic_clean, issue %in% econ.codes)
fp.policies <- filter(aic_clean, issue %in% fp.codes)
ne.policies <- filter(aic_clean, !(issue %in% econ.codes) & !(issue %in% fp.codes))


  #Generate Samples for Liberal and Conservative Policies
lib.policies <- filter(aic_clean, ideological_code > 4)
con.policies <- filter(aic_clean, ideological_code < 4)

#New Opinion Measures
aic_clean$elite.support <- aic_clean$pred90_sw*100
aic_clean$median.support <- aic_clean$pred50_sw*100
aic_clean$support.gap <- aic_clean$elite.support - aic_clean$median.support


##########################
#Create time variables in full dataset
##########################

#Cold War dummy var
aic_clean$pcw<-"w1_ColdWar"
aic_clean$pcw[aic_clean$year>1989]<-"w2_PostColdWar"

#Time windows (3 years, except for 1987-1990)
aic_clean$window<-0
aic_clean$window[aic_clean$year>=1981 & aic_clean$year<=1983]<-"w1_1981_1983"
aic_clean$window[aic_clean$year>=1984 & aic_clean$year<=1986]<-"w2_1984_1986"
aic_clean$window[aic_clean$year>=1987 & aic_clean$year<=1990]<-"w3_1987_1990"
aic_clean$window[aic_clean$year>=1991 & aic_clean$year<=1993]<-"w4_1991_1993"
aic_clean$window[aic_clean$year>=1994 & aic_clean$year<=1996]<-"w5_1994_1996"
aic_clean$window[aic_clean$year>=1997 & aic_clean$year<=1999]<-"w6_1997_1999"
aic_clean$window[aic_clean$year>=2000 & aic_clean$year<=2002]<-"w7_2000_2002"
stopifnot(!any(aic_clean$window==0))

##########################
#Create time variables in FP dataset
##########################

#Cold War dummy var
fp.policies$pcw<-"w1_ColdWar"
fp.policies$pcw[fp.policies$year>1989]<-"w2_PostColdWar"

#Time windows (Variable: 1981-1983; 1984-1990; 1991-1994; 1995-2000; 2001-2002)
fp.policies$window<-0
fp.policies$window[fp.policies$year>=1981 & fp.policies$year<=1983]<-"w1_1981_1983"
fp.policies$window[fp.policies$year>=1984 & fp.policies$year<=1990]<-"w2_1984_1990"
fp.policies$window[fp.policies$year>=1991 & fp.policies$year<=1994]<-"w3_1991_1994"
fp.policies$window[fp.policies$year>=1995 & fp.policies$year<=2000]<-"w4_1995_2000"
fp.policies$window[fp.policies$year>=2001 & fp.policies$year<=2002]<-"w5_2001_2002"
stopifnot(!any(fp.policies$window==0))

if(F){
	table(fp.policies$window,fp.policies$bioutcome)
}
##########################
#Create time variables in ECON dataset
##########################

#Cold War dummy var
econ.policies$pcw<-"w1_ColdWar"
econ.policies$pcw[econ.policies$year>1989]<-"w2_PostColdWar"

#Time windows (Variable: 1981-1983; 1984-1990; 1991-1995; 1996-2000; 2001-2002)
econ.policies$window<-0
econ.policies$window[econ.policies$year>=1981 & econ.policies$year<=1983]<-"w1_1981_1983"
econ.policies$window[econ.policies$year>=1984 & econ.policies$year<=1990]<-"w2_1984_1990"
econ.policies$window[econ.policies$year>=1991 & econ.policies$year<=1995]<-"w3_1991_1995"
econ.policies$window[econ.policies$year>=1996 & econ.policies$year<=2000]<-"w4_1996_2000"
econ.policies$window[econ.policies$year>=2001 & econ.policies$year<=2002]<-"w5_2001_2002"
stopifnot(!any(econ.policies$window==0))

if(F){
	table(econ.policies$window,econ.policies$bioutcome)
}

##########################
#Create time variables in NE dataset
##########################

#Cold War dummy var
ne.policies$pcw<-"w1_ColdWar"
ne.policies$pcw[ne.policies$year>1989]<-"w2_PostColdWar"

#Time windows (Variable: 1981-1989; 1990-1995; 1996-1999; 2000-2002)
ne.policies$window<-0
ne.policies$window[ne.policies$year>=1981 & ne.policies$year<=1989]<-"w1_1981_1989"
ne.policies$window[ne.policies$year>=1990 & ne.policies$year<=1995]<-"w2_1990_1995"
ne.policies$window[ne.policies$year>=1996 & ne.policies$year<=2002]<-"w3_1996_2002"
stopifnot(!any(ne.policies$window==0))

if(F){
	table(ne.policies$window,ne.policies$bioutcome)
}
